package com.atguigu.url.service.impl;

import com.atguigu.url.service.Err4xxStatisticsService;
import com.atguigu.url.util.JsonReadFromLocalFile;
import com.atguigu.url.util.MD5DigestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * @version V1.0
 * @author: guangcheng
 * @date: 2021/12/3 20:01
 * @description:
 */
@Service
public class Err4xxStatisticsServiceImpl implements Err4xxStatisticsService {

    @Value("${douyin.download.googlelogs}")
    String googlelogs;
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    JsonReadFromLocalFile jsonRead;
    @Autowired
    MD5DigestUtil md5DigestUtil;


    /**
     * 统计某一批次url的4xx情况
     *
     * @return
     */
    @Override
    public HashMap<String, String> getUrlWith4xx() {

        HashSet<String> targetWith4xxUrlSet = new HashSet<>();
        HashMap<String, String> urlMapWith4xx = new HashMap<>();

        //获取urlSet<String>
        Set<String> urlSet = jsonRead.getUrlSetFromJsonByLine(googlelogs);
        if (urlSet.size() > 0) {
            for (String url : urlSet) {
                String urlMd5 = md5DigestUtil.getMD5(url);
                // String val = urlMd5 + "_" + "4xx";
                // redisTemplate.opsForValue().set(urlMd5, val);
                String val = redisTemplate.opsForValue().get(urlMd5);
                if (!StringUtils.isEmpty(val) && val.contains("4xx")) {

                    urlMapWith4xx.put(urlMd5, url);
                    targetWith4xxUrlSet.add(url);
                }
            }
        }

        System.out.println("本批次url中4xx的数量为= " + targetWith4xxUrlSet.size());
        return urlMapWith4xx;
    }

}

